package cn.xiaoyu.su.manager.impl;

import cn.xiaoyu.su.enums.ShortLinkStateEnum;
import cn.xiaoyu.su.manager.GroupCodeMappingManager;
import cn.xiaoyu.su.mapper.GroupCodeMappingMapper;
import cn.xiaoyu.su.model.GroupCodeMappingDO;
import cn.xiaoyu.su.vo.GroupCodeMappingVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@Slf4j
public class GroupCodeMappingManagerImpl implements GroupCodeMappingManager {

    @Autowired
    private GroupCodeMappingMapper groupCodeMappingMapper;

    /**
     * 查找详情
     * @param mappingId
     * @param accountNo
     * @param groupId
     * @return
     */
    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long accountNo, Long groupId) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("id",mappingId).eq("account_no",accountNo)
                .eq("group_id",groupId));
    }

    /**
     * 新增
     * @param groupCodeMappingDO
     * @return
     */
    @Override
    public int add(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.insert(groupCodeMappingDO);
    }

    /**
     * 根据短链删除
     * @param groupCodeMappingDO
     * @return
     */
    @Override
    public int del(GroupCodeMappingDO groupCodeMappingDO) {
        //逻辑删除
        return groupCodeMappingMapper.update(null,new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id",groupCodeMappingDO.getId())
                .eq("account_no",groupCodeMappingDO.getAccountNo())
                .eq("group_id",groupCodeMappingDO.getGroupId())
                .set("del",1));
    }

    /**
     * 分页查找
     * @param page
     * @param size
     * @param accountNo
     * @param groupId
     * @return
     */
    @Override
    public Map<String, Object> pageShortLinkByGroupId(Integer page, Integer size, Long accountNo, Long groupId) {

        Page<GroupCodeMappingDO> pageInfo = new Page<>(page,size);

        Page<GroupCodeMappingDO> groupCodeMappingDOPage = groupCodeMappingMapper.selectPage(pageInfo, new QueryWrapper<GroupCodeMappingDO>()
                .eq("group_id", groupId).eq("del",0));

        Map<String,Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record",groupCodeMappingDOPage.getTotal());
        pageMap.put("total_page",groupCodeMappingDOPage.getPages());
        pageMap.put("current_data", groupCodeMappingDOPage.getRecords()
                .stream().map(obj -> beanProcess(obj))
                .collect(Collectors.toList()));

        return pageMap;
    }

    /**
     * 更新状态
     * @param accountNo
     * @param groupId
     * @param shortLinkCode
     * @param shortLinkStateEnum
     * @return
     */
    @Override
    public int updateGroupCodeMappingState(Long accountNo, Long groupId, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {
        return groupCodeMappingMapper.update(null,new UpdateWrapper<GroupCodeMappingDO>()
                .eq("account_no",accountNo).eq("group_id",groupId)
                .eq("code",shortLinkCode).eq("del",0)
                .set("state",shortLinkStateEnum.name()));
    }

    /**
     * 查询B端短链
     * @param shortLinkCode
     * @param groupId
     * @param accountNo
     * @return
     */
    @Override
    public GroupCodeMappingDO findByCodeAndGroupId(String shortLinkCode, Long groupId, Long accountNo) {
        return groupCodeMappingMapper.selectOne(new QueryWrapper<GroupCodeMappingDO>()
                .eq("code",shortLinkCode)
                .eq("group_id",groupId)
                .eq("account_no",accountNo)
                .eq("del",0));
    }

    /**
     * 更新
     * @param groupCodeMappingDO
     * @return
     */
    @Override
    public int update(GroupCodeMappingDO groupCodeMappingDO) {
        return groupCodeMappingMapper.update(null,new UpdateWrapper<GroupCodeMappingDO>()
                .eq("id",groupCodeMappingDO.getId())
                .eq("account_no",groupCodeMappingDO.getAccountNo())
                .eq("group_id",groupCodeMappingDO.getGroupId())
                .eq("del",0)

                .set("title",groupCodeMappingDO.getTitle())
                .set("domain",groupCodeMappingDO.getDomain()));
    }

    private GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO) {
        GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
        BeanUtils.copyProperties(groupCodeMappingDO,groupCodeMappingVO);
        return groupCodeMappingVO;
    }
}
